

const liveness = require('../../config/global_config').liveness;
const Config = require('./config');
const httpUtil = require('../utils/http');
const Cons = require('../common/constants');
const logger = require('../utils/logger');
/*

*/
async function check_filter(channel, message) {
    let config = await Config.getLiveness();
    if (!config || !config.detect) {
        logger.logInfo("\ndo not check liveness, passed\n");
        message.liveness = Cons.RECORD_LIVENESS_NOT_CHECK;
        return message;
    }
    try {
        let msg = message;
        if (!msg || !msg.face_info || !msg.face_info.r_cp) {
            logger.logInfo("\n message not matched, passed check liveness.\n");
            return message;
        }
        let res = await httpUtil.get(liveness.detect + '?picture=' + msg.face_info.r_cp);
        logger.logInfo("\n check liveness result : " + JSON.stringify(res));
        // TODO json
        if (res && res.liveness) {
            message.liveness = Cons.RECORD_LIVENESS_LIVE;
            return message;
        }
        message.liveness = Cons.RECORD_LIVENESS_NOT_LIVE;
        return message;
    } catch (err) {
        message.liveness = Cons.RECORD_LIVENESS_ERROR;
        logger.logError("\n" + '===== Filter error - ' + err + ' =====' + "\n");
        return message;
    }
}

exports.check_filter = check_filter;